class SearchController < ApplicationController
  ITEMS_PER_PAGE = 10
  
  def index
    @page_title = l(:search, :title) + " \"" + params[:q] + "\""
    
    q = params[:q]
    if q == nil
      q = ''
    end
    
    #this is the query that the user submitted
    @query = q
    
    parts = q.split(%r{\s+})
    q = ''
    if parts != nil && parts.size > 0
      for i in (0..parts.size-1)
        p = parts[i]
        if i != 0
          q += ' OR '
        end
        q += p + '~'
      end
    end

    #this is the current page that the user is at
    @page = (params[:page] == nil) ? 1 : params[:page].to_i
    
    #this is the offset into the total set of results.  it's 0 to start
    @offset = (@page - 1) * ITEMS_PER_PAGE
    
    create_results q
    create_page_variables
    
    #this is the last result that is displayed
    @end = (@offset + ITEMS_PER_PAGE <= @size) ? @offset + ITEMS_PER_PAGE : @size
    
    if @end > @size
      @end -= 1
    end
  end

  def create_page_variables

    #determine what the last page is
    if @size % ITEMS_PER_PAGE == 0
      last_page = @size / ITEMS_PER_PAGE
    else
      last_page = @size / ITEMS_PER_PAGE + 1
    end
    
    #determine the value of page1, -1 if it shouldn't be displayed
    if @page > 1
      @page1 = @page - 1
    else
      @page1 = @page
    end
    
    #determine the value of page2, -1 if it shouldn't be displayed
    if @page == 1 && @page < last_page
        @page2 = 2
    elsif @page <= last_page && last_page != 1
      @page2 = @page
    else
      @page2 = -1
    end
    
    #determine the value of page3, -1 if it shouldn't be displayed
    if @page == 1 && @page < last_page - 1
      @page3 = 3
    elsif @page <= last_page - 1
      @page3 = @page + 1
    else
      @page3 = -1
    end
    
    #determine if the "prev" link should be there
    @show_prev = @page > 1
    #determine if the "next" link should be there
    @show_next = @page <= last_page - 1
    #determine if any pages at all should be there
    @show_any = last_page != 1 && @size > 0
  end

  def create_results q
    #query each place that we want to search
    artwork = Artwork.find_by_contents(q)
    artwork_desc = ArtworkDescription.find_by_contents(q)
    #categories = Category.find_by_contents(q)
    regions = Region.find_by_contents(q)
    artists = Artist.find_by_contents(q)
    artists_desc = ArtistDescription.find_by_contents(q)
    
    @results = artwork
    
    #for each result array, grab the artwork
    
    # handle artwork descriptions
    for t in artwork_desc
      @results.push(t.artwork)
    end

    # handle categories
    #for t in categories
#      for t2 in artwork
#        @results.push(t2)
#      end
    #end
    
    # handle regions
    for t in regions
      for t2 in t.artists
        for t3 in t2.artworks
          @results.push(t3)
        end
      end
    end

    # handle artists
    for t in artists
      for t2 in t.artworks
        @results.push(t2)
      end
    end
    
    # handle artist descriptions
    for t in artists_desc
      for t2 in t.artist.artworks
        @results.push(t2)
      end
    end
    
    #get rid of duplicates
    @results = @results.uniq
    #get the total number of results
    @size = @results.length
    
    #reassign results to keep the page in mind
    @results = @results[@offset, ITEMS_PER_PAGE]
  end

end
